package com.jdbc;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 测试c3p0连接池
 * 依赖jar包
 * 1.c3p0-0.9.5.1.jar
 * 2.mchange-commons-java-0.2.10.jar
 * @author lovemanai
 *
 */
public class TestC3p0ConnectionPool {
	
	@Test
	public void Test1() throws PropertyVetoException, SQLException {
		
		//创建连接池对象
		ComboPooledDataSource ds = new ComboPooledDataSource();
		
		//配置四大参数
		ds.setDriverClass("com.mysql.jdbc.Driver");
		ds.setJdbcUrl("jdbc:mysql://localhost:3306/testdatabase");
		ds.setUser("root");
		ds.setPassword("root");
		
		//配置连接池数参数,也可以是用默认参数
		ds.setAcquireIncrement(5);
		ds.setMaxPoolSize(50);
		
		//得到连接对象
		Connection conn = ds.getConnection();
		
		//com.mchange.v2.c3p0.impl.NewProxyConnection
		System.out.println(conn.getClass().getName());
	}
	
	/**
	 * 使用默认配置
	 * @throws SQLException 
	 */
	@Test
	public void Test2() throws SQLException {
		ComboPooledDataSource ds = new ComboPooledDataSource();
		Connection conn = ds.getConnection();
		System.out.println(conn.getClass().getName());
		conn.close();
	}
	/**
	 * 使用命名配置信息
	 * @throws SQLException 
	 * 
	 */
	public void Test3() throws SQLException {
		/*
		 * 构造器的指定参数指定命名配置信息
		 * <named-config name="oracle-config"> 
		 */
		ComboPooledDataSource ds = new ComboPooledDataSource("oracle-config");
		Connection conn = ds.getConnection();
		System.out.println(conn.getClass().getName());
		conn.close();
		
	}
}
